﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Linq;

public partial class Pages_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LoginUser_LoggedIn(object sender, EventArgs e)
    {
        Login login = (Login)sender;
        AddSessionData(login);
    }

    private void AddSessionData(Login login)
    {
        string[] roleName = Roles.GetRolesForUser(login.UserName);
        Session.Add("userName", login.UserName);

        IList<string> permissionList = GetPermissions(roleName);

        if (permissionList != null && permissionList.Count > 0)
        {
            Session.Add("premissionList", permissionList);
        }
    }

    protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
    {
        bool rememberMe = ((CheckBox)((Login)sender).FindControl("RememberMe")).Checked;
        e.Authenticated = Membership.ValidateUser(LoginUser.UserName, LoginUser.Password);

        if (e.Authenticated)
        {
            FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, rememberMe);
        }
    }

    private IList<string> GetPermissions(string[] roleNameArr)
    {
        string roleName = string.Empty;
        if (roleNameArr.Count() > 0 && roleNameArr != null)
        {
            foreach (string item in roleNameArr)
            {
                roleName += "'" + item + "', ";
            }
        }

        roleName = roleName.Substring(0, roleName.Length - 2);

        DataContext dc = new DataContext(DBConnection.ConnectionString);
        string sql;
        sql = string.Format("Select Distinct(PageName) as PageName from RolePagePermissionMap where RoleName in (" + roleName+" )");
        IList<string> permissionList = dc.ExecuteQuery<string>(@sql).ToList();
        return permissionList;
    }
    //    IList<RolePagePermissionMap> permissionList = new List<RolePagePermissionMap>();

    //    SqlConnection sqlConnection = new SqlConnection(DBConnection.ConnectionString);
    //    SqlCommand sqlCommand = new SqlCommand();
    //    string sql;
    //    sql = string.Format("select * from RolePagePermissionMap where RoleName = " + roleName);
    //    sqlCommand.CommandText = sql;
    //    sqlCommand.Connection = sqlConnection;
    //    sqlCommand.CommandType = CommandType.Text;
    //    try
    //    {
    //        sqlConnection.Open();

    //        SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
    //        while (sqlDataReader.Read())
    //        {
    //            RolePagePermissionMap permission= new RolePagePermissionMap();
    //            permission.OID = new Guid(sqlDataReader["OID"].ToString());
    //            permission.RoleName = sqlDataReader["RoleName"].ToString();
    //            permission.PageName = sqlDataReader["PageName"].ToString();
    //            permissionList.Add(permission);
    //        }


    //        sqlDataReader.Close();
    //    }
    //    catch (Exception ex)
    //    {
    //        return null;
    //    }
    //    finally
    //    {
    //        sqlConnection.Close();
    //    }
    //    return permissionList;
    //}
}
